package com.app.service.Impl;

import com.app.mapper.ComplaintMapper;
import com.app.mapper.VisitRecordsMapper;
import com.app.model.VisitRecordModel;
import com.app.service.VisitRecordsService;
import com.app.util.CommonHelp;
import com.app.vo.VisitRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VisitRecordsServiceImpl implements VisitRecordsService {
    @Autowired
    VisitRecordsMapper visitRecordsMapper;

    @Override
    public void insertAndUpdateVisit(VisitRecordModel visitRecordModel) {

        VisitRecordModel visitRecord = visitRecordsMapper.selectById(visitRecordModel.getId());
        if (visitRecord == null){
            visitRecordModel.setCreateTime(CommonHelp.getNowTimestamp());
            visitRecordsMapper.insert(visitRecordModel);
        }else {
            visitRecordModel.setUpdateTime(CommonHelp.getNowTimestamp());
            visitRecordsMapper.updateById(visitRecordModel);
        }
    }

    @Override
    public VisitRecordVo getVisitRecordsList(Integer complaintId,String beginTime,String endTime,Integer page, Integer limit) {
        IPage<VisitRecordModel> page1 = new Page<>(page,limit);
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("complaint_id",complaintId);

        if (beginTime != "" || endTime !=""){
            beginTime=beginTime+" 00:00:00";
            endTime=endTime+" 23:59:00";
            queryWrapper.ge("visit_time",beginTime);
            queryWrapper.le("visit_time",endTime);
        }

        IPage iPage = visitRecordsMapper.selectPage(page1, queryWrapper);
        List list = iPage.getRecords();
        int count = (int) iPage.getTotal();
        VisitRecordVo vo = new VisitRecordVo();
        vo.setList(list);
        vo.setCount(count);
        return vo;
    }

    @Override
    public VisitRecordModel getVisitRecordsById(Integer id) {
        VisitRecordModel visitRecordModel = visitRecordsMapper.selectById(id);
        return visitRecordModel;
    }

    @Override
    public void deleteVisitRecords(Integer id) {
        visitRecordsMapper.deleteById(id);
    }
}
